//
//  InspectionCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class InspectionCell: UITableViewCell {
  static func height(with model: Inspection) -> CGFloat {
    var height = 80 + UIFont.calculateHeight(width: UIScreen.width - 56, font: GlobalConfig.system16Font, text: model.region)
    if model.cooperate {
        height += 24
    }
    return height
  }
  
  static var identifier: String { return "InspectionCell.swxc" }
  
  var model: Inspection? {
    didSet {
      guard let temp = model else { return }
      
      if temp.timeInterval.isEmpty { areaLabel.text = "" }
      else {
        areaLabel.text = temp.timeInterval + "巡检"
      }
    
      contentLabel.text = temp.areaName
      
      if "WaitInspection" == temp.status {
        
        stateImageV.isHidden = true
        areaLabel.textColor = GlobalConfig.BGSkyBlueColor
        contentLabel.textColor = GlobalConfig.TextDeepColor
        
      } else if "LackInspection" == temp.status {
      
        stateImageV.isHidden = false
        stateImageV.image = UIImage(named: "show-list-icon-blue")
        areaLabel.textColor = GlobalConfig.BGSkyBlueColor
        contentLabel.textColor = GlobalConfig.TextDeepColor
        
      } else if "Inspected" == temp.status {
        
        stateImageV.isHidden = false
        stateImageV.image = UIImage(named: "show-list-icon-grey")
        areaLabel.textColor = GlobalConfig.TextLightColor
        contentLabel.textColor = GlobalConfig.TextLightColor
      }
        
        if temp.cooperate {
            togertherLabel.isHidden = false
            togetherImageView.isHidden = false
        }
        else {
            togertherLabel.isHidden = true
            togetherImageView.isHidden = true
        }
      
    }
  }
  
  fileprivate var containerV: UIView!
  fileprivate var areaLabel: UILabel!
  fileprivate var contentLabel: UILabel!
  fileprivate var stateImageV: UIImageView!
    
    fileprivate var togetherImageView: UIImageView!
    fileprivate var togertherLabel: UILabel!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension InspectionCell {
  fileprivate func create() {
    containerV = UIView()
    contentView.addSubview(containerV)
    containerV.backgroundColor = UIColor.white
    containerV.snp.makeConstraints { (make) in
      make.left.equalTo(self).offset(10)
      make.right.equalTo(self).offset(-10)
      make.top.equalTo(self).offset(2.5)
      make.bottom.equalTo(self).offset(-2.5)
    }
    containerV.layer.cornerRadius = 3
    containerV.clipsToBounds = true
    
    areaLabel = UILabel()
    containerV.addSubview(areaLabel)
    areaLabel.font = GlobalConfig.system15Font
    areaLabel.textColor = GlobalConfig.BGSkyBlueColor
    areaLabel.snp.makeConstraints { (make) in
      make.top.equalTo(containerV).offset(20)
      make.left.equalTo(containerV).offset(18)
    }
    
    contentLabel = UILabel()
    containerV.addSubview(contentLabel)
    contentLabel.numberOfLines = 0
    contentLabel.font = GlobalConfig.system16Font
    contentLabel.textColor = GlobalConfig.TextDeepColor
    contentLabel.snp.makeConstraints { (make) in
      make.left.equalTo(containerV).offset(18)
      make.right.equalTo(containerV).offset(-18)
      make.top.equalTo(areaLabel.snp.bottom).offset(20)
    }
    
    stateImageV = UIImageView()
    containerV.addSubview(stateImageV)
    stateImageV.snp.makeConstraints { (make) in
      make.top.right.equalToSuperview()
      make.width.height.equalTo(30)
    }
    
    togetherImageView = UIImageView()
    togetherImageView.image = UIImage(named: "cooperateInspection")
    containerV.addSubview(togetherImageView)
    togetherImageView.snp.makeConstraints { (make) in
        make.left.equalTo(18)
        make.bottom.equalTo(-20)
        make.height.width.equalTo(14)
    }
    togetherImageView.isHidden = true
    
    togertherLabel = UILabel.getLabel(with: GlobalConfig.system14Font, textColor: GlobalConfig.TextLightColor)
    containerV.addSubview(togertherLabel)
    togertherLabel.text = "协作巡检"
    togertherLabel.snp.makeConstraints { (make) in
        make.centerY.equalTo(togetherImageView)
        make.left.equalTo(togetherImageView.snp.right).offset(6)
    }
    togertherLabel.isHidden = true
    
  }
}
